<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
    <meta http-equiv="content-language" content="zh-CN" />
    

    
    <meta name="viewport" content="width=device-width, initial-scale=0.5">
    

    
    <title>R语言字符处理函数包---stringr包</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
    
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css">

    <link rel="stylesheet" href="/css/stylesheet.css">
    <link rel="stylesheet" href="/css/home.css">

    
    
        <style type="text/css">
        body { background-color: #fbf6ec;}
        </style>
    
    
                
        
        
            <link rel="stylesheet" href="/css/main.css"/>
        




        
        
        
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/styles/github.min.css"  />
         
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/highlight.min.js"></script>
        
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/r.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/yaml.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/latex.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/matlab.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/mathematica.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia-repl.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/powershell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/bash.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/shell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/python.min.js"></script>
        
        <script>hljs.initHighlightingOnLoad();</script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
     
     
</head>


<body>
    <script>
        window.addEventListener("resize", resizeThrottler, false);

        var resizeTimeout;
        function resizeThrottler() {
        
        if ( !resizeTimeout ) {
            resizeTimeout = setTimeout(function() {
            resizeTimeout = null;
            actualResizeHandler();
        
            
            }, 66);
        }
        }
        actualResizeHandler()
        function actualResizeHandler() {
                if (/mobile/i.test(navigator.userAgent) || /android/i.test(navigator.userAgent))
                {
                    document.body.classList.add('mobile');
                }else{
                    document.body.classList.remove('mobile');  
                }
    }</script>

    
      
      
            <nav class="navbar navbar-default navbar-static-top" style="opacity: .9" role="navigation">
        <div class="container-fluid">
            
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">

                    <span class="sr-only">Toggle navigation</span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>

                </button>
                <a class="navbar-brand" href="/">zsc</a>
            </div>

            <div class="navbar-collapse collapse" id="bs-example-navbar-collapse-1" style="height: auto;">
                <ul class="nav navbar-nav navbar-right" style="font-size: 100%">
                    
                        
                            
                            <li class=""><a href="/about/">About</a></li>
                            
                            <li class=""><a href="/categories/">Categories</a></li>
                            
                            <li class=""><a href="/">Home</a></li>
                            
                            <li class=""><a href="/tags/">Tags</a></li>
                            
                            <li class=""><a href="/issue/">存在的问题</a></li>
                            
                        
                    
                </ul>
            </div>
        </div>
    </nav>










<div class="inner">
    



    <div class="blog-post">
        
                <div>
            <h2 align="center" id = "singe-h2">
                R语言字符处理函数包---stringr包
                <time>
                    <br>
                    <span> 
                        <i class="fa fa-user-edit" style="color:#888;font-size: 80%;"></i>
                        zsc 
                    </span>
                    &nbsp 
                    <span>                 
                        <i class="fa fa-calendar-alt" style="color:#888;font-size: 80%;"></i>
                        2018-04-05 
                    </span>
                </time>
                
                
                <div>
                    <ul class="tags">
                        
                        <span>标签:</span>
                        <li><a class="link" href="/tags/r"> #r </a></li><li><a class="link" href="/tags/stringr"> #stringr </a></li>
                        
                        <span> </span>
                        
                    </ul>
                    
                </div>
            </h2>
        </div>
    
        
        <section id="content">
            <h2 id="1stringr介绍">1.stringr介绍</h2>
<p>stringr包被定义为一致的、简单易用的字符串工具集。所有的函数和参数定义都具有一致性，比如，用相同的方法进行NA处理和0长度的向量处理。</p>
<p>字符串处理虽然不是R语言中最主要的功能，却也是必不可少的，数据清洗、可视化等的操作都会用到。对于R语言本身的base包提供的字符串基础函数，随着时间的积累，已经变得很多地方不一致，不规范的命名，不标准的参数定义，很难看一眼就上手使用。字符串处理在其他语言中都是非常方便的事情，R语言在这方面确实落后了。stringr包就是为了解决这个问题，让字符串处理变得简单易用，提供友好的字符串操作接口。</p>
<h2 id="2stringr的api介绍">2.stringr的API介绍</h2>
<p>stringr包常用的字符串的处理以str_开头来命名，方便更直观理解函数的定义。我们可以根据使用习惯对函数进行分类：</p>
<h3 id="字符串拼接函数"><strong>字符串拼接函数</strong></h3>
<table>
<thead>
<tr>
<th>函数</th>
<th>说明</th>
<th>R Base中对应函数</th>
</tr>
</thead>
<tbody>
<tr>
<td>str_c()<br>str_join() 同str_c()</td>
<td>字符串拼接</td>
<td>paste(),paste0()</td>
</tr>
<tr>
<td>str_trim())</td>
<td>去掉字符串的空格和TAB(\t)</td>
<td></td>
</tr>
<tr>
<td>str_pad()</td>
<td>用pad参数填充字符串的长度，长度自己设定</td>
<td></td>
</tr>
<tr>
<td>str_dup()</td>
<td>复制字符串</td>
<td></td>
</tr>
<tr>
<td>str_wrap()</td>
<td>控制字符串输出格式</td>
<td>strtrim()</td>
</tr>
<tr>
<td>str_sub()<br>str_sub() &lt;- value</td>
<td>截取字符串<br>抽取或替换正则表达式匹配子串</td>
<td>regmatches()<br>regmatches() &lt;- value</td>
</tr>
</tbody>
</table>
<h3 id="字符串计算函数"><strong>字符串计算函数</strong></h3>
<table>
<thead>
<tr>
<th>函数</th>
<th>说明</th>
<th>R Base中对应函数</th>
</tr>
</thead>
<tbody>
<tr>
<td>str_count()</td>
<td>字符串计数，返回指定匹配出现的次数</td>
<td></td>
</tr>
<tr>
<td>str_length()</td>
<td>返回字符的长度</td>
<td>nchar()</td>
</tr>
<tr>
<td>str_sort()</td>
<td>对字符值进行排序</td>
<td></td>
</tr>
<tr>
<td>str_order()</td>
<td>字符串索引排序，</td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="字符串匹配函数"><strong>字符串匹配函数</strong></h3>
<table>
<thead>
<tr>
<th>函数</th>
<th>说明</th>
<th>R Base中对应函数</th>
</tr>
</thead>
<tbody>
<tr>
<td>str_split()<br>str_split_fixed() 同上</td>
<td>按照模式分割字符串</td>
<td>strsplit()</td>
</tr>
<tr>
<td>str_match()<br>str_match_all()</td>
<td>从字符串中提取首个匹配组<br></td>
<td></td>
</tr>
<tr>
<td>str_replace<br>str_replace_all</td>
<td>替换首个匹配模式<br></td>
<td>sub()<br>gsub()</td>
</tr>
<tr>
<td>str_replace_na()</td>
<td>把NA替换为NA字符串</td>
<td></td>
</tr>
<tr>
<td>str_locate()<br>str_locate_all()</td>
<td>返回首个匹配模式的字符的位置<br></td>
<td>regexpr()<br>gregexpr()</td>
</tr>
<tr>
<td>str_extract()<br>str_extract_all()</td>
<td>提取首个匹配模式的字符<br></td>
<td></td>
</tr>
<tr>
<td>str_subset()</td>
<td>返回匹配的字符串</td>
<td></td>
</tr>
<tr>
<td>str_detect()</td>
<td>检测字符是否存在某些指定模式</td>
<td>grepl()</td>
</tr>
<tr>
<td>word()</td>
<td>从文本中提取单词</td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="字符串变换函数"><strong>字符串变换函数</strong></h3>
<table>
<thead>
<tr>
<th>函数</th>
<th>说明</th>
<th>R Base中对应函数</th>
</tr>
</thead>
<tbody>
<tr>
<td>str_conv()</td>
<td>字符编码转换,基本函数中的比较好用</td>
<td>iconv(x, from = &ldquo;&rdquo;, to = &ldquo;&rdquo;)</td>
</tr>
<tr>
<td>str_to_upper()<br>str_to_lower()</td>
<td>字符串转成大写/小写</td>
<td>tolower(x)<br>toupper(x)</td>
</tr>
<tr>
<td>str_to_title()<br>同大小写转换</td>
<td>字符串转成首字母大写</td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="参数控制函数"><strong>参数控制函数</strong>，</h3>
<p>仅用于构造功能的参数，不能独立使用。</p>
<ul>
<li>boundary:	定义使用边界</li>
<li>coll:	       定义字符串标准排序规则。</li>
<li>fixed:	定义用于匹配的字符，包括正则表达式中的转义符</li>
<li>regex:	定义正则表达式</li>
</ul>
<h2 id="3stringr的详细使用">3.stringr的详细使用</h2>
<p><strong>3.1 字符串拼接函数</strong></p>
<p><strong>3.1.1 str_c，字符串拼接操作，与str_join完全相同，与paste()行为不完全一致。</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_c</span>(<span style="color:#66d9ef">...</span>, sep <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>, collapse <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_join</span>(<span style="color:#66d9ef">...</span>, sep <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>, collapse <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>…: 多参数的输入</li>
<li>sep: 把多个字符串拼接为一个大的字符串，用于字符串的分割符。</li>
<li>collapse: 把多个向量参数拼接为一个大的字符串，用于字符串的分割符。</li>
</ul>
<p>把多个字符串拼接为一个大的字符串。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;b&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;ab&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;b&#39;</span>,sep<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;-&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;a-b&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;a1&#39;</span>),<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#39;b&#39;</span>,<span style="color:#e6db74">&#39;b1&#39;</span>),sep<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;-&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;a-b&#34;</span>   <span style="color:#e6db74">&#34;a1-b1&#34;</span>
</span></span></code></pre></div><p>把多个向量参数拼接为一个大的字符串。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#a6e22e">head</span>(<span style="color:#66d9ef">letters</span>), collapse <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abcdef&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#a6e22e">head</span>(<span style="color:#66d9ef">letters</span>), collapse <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;, &#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;a, b, c, d, e, f&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># collapse参数，对多个字符串无效</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;b&#39;</span>,collapse <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;-&#34;</span>)   
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;ab&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;a1&#39;</span>),<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#39;b&#39;</span>,<span style="color:#e6db74">&#39;b1&#39;</span>),collapse<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;-&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;ab-a1b1&#34;</span>
</span></span></code></pre></div><p>拼接有NA值的字符串向量时，NA还是NA</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;a&#34;</span>, <span style="color:#66d9ef">NA</span>, <span style="color:#e6db74">&#34;b&#34;</span>), <span style="color:#e6db74">&#34;-d&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;a-d&#34;</span> <span style="color:#66d9ef">NA</span>    <span style="color:#e6db74">&#34;b-d&#34;</span>
</span></span></code></pre></div><p>对比str_c()函数和paste()函数之间的不同点。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#75715e"># 多字符串拼接，默认的sep参数行为不一致</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;b&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;ab&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">paste</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;b&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;a b&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 向量拼接字符串，collapse参数的行为一致</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#a6e22e">head</span>(<span style="color:#66d9ef">letters</span>), collapse <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abcdef&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">paste</span>(<span style="color:#a6e22e">head</span>(<span style="color:#66d9ef">letters</span>), collapse <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abcdef&#34;</span>
</span></span><span style="display:flex;"><span> 
</span></span><span style="display:flex;"><span><span style="color:#75715e">#拼接有NA值的字符串向量，对NA的处理行为不一致</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_c</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;a&#34;</span>, <span style="color:#66d9ef">NA</span>, <span style="color:#e6db74">&#34;b&#34;</span>), <span style="color:#e6db74">&#34;-d&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;a-d&#34;</span> <span style="color:#66d9ef">NA</span>    <span style="color:#e6db74">&#34;b-d&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">paste</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;a&#34;</span>, <span style="color:#66d9ef">NA</span>, <span style="color:#e6db74">&#34;b&#34;</span>), <span style="color:#e6db74">&#34;-d&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;a -d&#34;</span>  <span style="color:#e6db74">&#34;NA -d&#34;</span> <span style="color:#e6db74">&#34;b -d&#34;</span> 
</span></span></code></pre></div><p><strong>3.1.2 str_trim:去掉字符串的空格和TAB(\t)</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_trim</span>(string, side <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;both&#34;</span>, <span style="color:#e6db74">&#34;left&#34;</span>, <span style="color:#e6db74">&#34;right&#34;</span>))
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>side: 过滤方式，both两边都过滤，left左边过滤，right右边过滤</li>
</ul>
<p>去掉字符串的空格和TAB(\t)</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#75715e">#只过滤左边的空格</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_trim</span>(<span style="color:#e6db74">&#34;  left space\t\n&#34;</span>,side<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;left&#39;</span>) 
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;left space\t\n&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#只过滤右边的空格</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_trim</span>(<span style="color:#e6db74">&#34;  left space\t\n&#34;</span>,side<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;right&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;  left space&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#过滤两边的空格</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_trim</span>(<span style="color:#e6db74">&#34;  left space\t\n&#34;</span>,side<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;both&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;left space&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#过滤两边的空格</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_trim</span>(<span style="color:#e6db74">&#34;\nno space\n\t&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;no space&#34;</span>
</span></span></code></pre></div><p><strong>3.1.3 str_pad:补充字符串的长度</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_pad</span>(string, width, side <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;left&#34;</span>, <span style="color:#e6db74">&#34;right&#34;</span>, <span style="color:#e6db74">&#34;both&#34;</span>), pad <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34; &#34;</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>width: 字符串填充后的长度</li>
<li>side: 填充方向，both两边都填充，left左边填充，right右边填充</li>
<li>pad: 用于填充的字符</li>
</ul>
<p>补充字符串的长度。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#75715e"># 从左边补充空格，直到字符串长度为20</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_pad</span>(<span style="color:#e6db74">&#34;conan&#34;</span>, <span style="color:#ae81ff">20</span>, <span style="color:#e6db74">&#34;left&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;               conan&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 从右边补充空格，直到字符串长度为20</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_pad</span>(<span style="color:#e6db74">&#34;conan&#34;</span>, <span style="color:#ae81ff">20</span>, <span style="color:#e6db74">&#34;right&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;conan               &#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 从左右两边各补充空格，直到字符串长度为20</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_pad</span>(<span style="color:#e6db74">&#34;conan&#34;</span>, <span style="color:#ae81ff">20</span>, <span style="color:#e6db74">&#34;both&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;       conan        &#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 从左右两边各补充x字符，直到字符串长度为20</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_pad</span>(<span style="color:#e6db74">&#34;conan&#34;</span>, <span style="color:#ae81ff">20</span>, <span style="color:#e6db74">&#34;both&#34;</span>,<span style="color:#e6db74">&#39;x&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;xxxxxxxconanxxxxxxxx&#34;</span>
</span></span></code></pre></div><p><strong>3.1.4 str_dup: 复制字符串</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_dup</span>(string, times)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>times: 复制数量</li>
</ul>
<p>复制一个字符串向量。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abca4&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba2&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 复制2次</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_dup</span>(val, <span style="color:#ae81ff">2</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abca4abca4&#34;</span> <span style="color:#e6db74">&#34;123123&#34;</span>     <span style="color:#e6db74">&#34;cba2cba2&#34;</span>  
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 按位置复制</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_dup</span>(val, <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#ae81ff">3</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abca4&#34;</span>        <span style="color:#e6db74">&#34;123123&#34;</span>       <span style="color:#e6db74">&#34;cba2cba2cba2&#34;</span>
</span></span></code></pre></div><p><strong>3.1.5 str_wrap，控制字符串输出格式</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_wrap</span>(string, width <span style="color:#f92672">=</span> <span style="color:#ae81ff">80</span>, indent <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, exdent <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>width: 设置一行所占的宽度。</li>
<li>indent: 段落首行的缩进值</li>
<li>exdent: 段落非首行的缩进值</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span> txt<span style="color:#f92672">&lt;-</span><span style="color:#e6db74">&#39;R语言作为统计学一门语言，一直在小众领域闪耀着光芒。直到大数据的爆发，R语言变成了一门炙手可热的数据分析的利器。随着越来越多的工程背景的人的加入，R语言的社区在迅速扩大成长。现在已不仅仅是统计领域，教育，银行，电商，互联网….都在使用R语言。&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 设置宽度为40个字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">cat</span>(<span style="color:#a6e22e">str_wrap</span>(txt, width <span style="color:#f92672">=</span> <span style="color:#ae81ff">40</span>), <span style="color:#e6db74">&#34;\n&#34;</span>)
</span></span><span style="display:flex;"><span>R语言作为统计学一门语言，一直在小众领域
</span></span><span style="display:flex;"><span>闪耀着光芒。直到大数据的爆发，R语言变成
</span></span><span style="display:flex;"><span>了一门炙手可热的数据分析的利器。随着越来
</span></span><span style="display:flex;"><span>越多的工程背景的人的加入，R语言的社区在
</span></span><span style="display:flex;"><span>迅速扩大成长。现在已不仅仅是统计领域，教
</span></span><span style="display:flex;"><span>育，银行，电商，互联网….都在使用R语言。 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 设置宽度为60字符，首行缩进2字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">cat</span>(<span style="color:#a6e22e">str_wrap</span>(txt, width <span style="color:#f92672">=</span> <span style="color:#ae81ff">60</span>, indent <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>), <span style="color:#e6db74">&#34;\n&#34;</span>)
</span></span><span style="display:flex;"><span>  R语言作为统计学一门语言，一直在小众领域闪耀着光芒。直到大数
</span></span><span style="display:flex;"><span>据的爆发，R语言变成了一门炙手可热的数据分析的利器。随着越来
</span></span><span style="display:flex;"><span>越多的工程背景的人的加入，R语言的社区在迅速扩大成长。现在已
</span></span><span style="display:flex;"><span>不仅仅是统计领域，教育，银行，电商，互联网….都在使用R语言。 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 设置宽度为10字符，非首行缩进4字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">cat</span>(<span style="color:#a6e22e">str_wrap</span>(txt, width <span style="color:#f92672">=</span> <span style="color:#ae81ff">10</span>, exdent <span style="color:#f92672">=</span> <span style="color:#ae81ff">4</span>), <span style="color:#e6db74">&#34;\n&#34;</span>)
</span></span><span style="display:flex;"><span>R语言作为
</span></span><span style="display:flex;"><span>    统计学一
</span></span><span style="display:flex;"><span>    门语言，
</span></span><span style="display:flex;"><span>    一直在小
</span></span><span style="display:flex;"><span>    众领域闪
</span></span><span style="display:flex;"><span>    耀着光芒。
</span></span><span style="display:flex;"><span>    直到大数据
</span></span><span style="display:flex;"><span>    的爆发，R
</span></span><span style="display:flex;"><span>    语言变成了
</span></span><span style="display:flex;"><span>    一门炙手可
</span></span><span style="display:flex;"><span>    热的数据分
</span></span><span style="display:flex;"><span>    析的利器。
</span></span><span style="display:flex;"><span>    随着越来
</span></span><span style="display:flex;"><span>    越多的工程
</span></span><span style="display:flex;"><span>    背景的人的
</span></span><span style="display:flex;"><span>    加入，R语
</span></span><span style="display:flex;"><span>    言的社区在
</span></span><span style="display:flex;"><span>    迅速扩大成
</span></span><span style="display:flex;"><span>    长。现在已
</span></span><span style="display:flex;"><span>    不仅仅是统
</span></span><span style="display:flex;"><span>    计领域，教
</span></span><span style="display:flex;"><span>    育，银行，
</span></span><span style="display:flex;"><span>    电商，互联
</span></span><span style="display:flex;"><span>    网….都在使
</span></span><span style="display:flex;"><span>    用R语言。 
</span></span></code></pre></div><p><strong>3.1.6 str_sub,截取字符串</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_sub</span>(string, start <span style="color:#f92672">=</span> <span style="color:#ae81ff">1L</span>, end <span style="color:#f92672">=</span> <span style="color:#ae81ff">-1L</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>start : 开始位置</li>
<li>end : 结束位置</li>
</ul>
<p>截取字符串。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> txt <span style="color:#f92672">&lt;-</span> <span style="color:#e6db74">&#34;I am Conan.&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 截取1-4的索引位置的字符串</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(txt, <span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">4</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;I am&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 截取1-6的索引位置的字符串</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(txt, end<span style="color:#f92672">=</span><span style="color:#ae81ff">6</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;I am C&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 截取6到结束的索引位置的字符串</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(txt, <span style="color:#ae81ff">6</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;Conan.&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 分2段截取字符串</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(txt, <span style="color:#a6e22e">c</span>(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">4</span>), <span style="color:#a6e22e">c</span>(<span style="color:#ae81ff">6</span>, <span style="color:#ae81ff">8</span>))
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;I am C&#34;</span> <span style="color:#e6db74">&#34;m Con&#34;</span> 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 通过负坐标截取字符串</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(txt, <span style="color:#ae81ff">-3</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;an.&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(txt, end <span style="color:#f92672">=</span> <span style="color:#ae81ff">-3</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;I am Cona&#34;</span>
</span></span></code></pre></div><p>对截取的字符串进行赋值。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> x <span style="color:#f92672">&lt;-</span> <span style="color:#e6db74">&#34;AAABBBCCC&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 在字符串的1的位置赋值为1</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(x, <span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">1</span>) <span style="color:#f92672">&lt;-</span> <span style="color:#ae81ff">1</span>; x
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;1AABBBCCC&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 在字符串从2到-2的位置赋值为2345</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sub</span>(x, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">-2</span>) <span style="color:#f92672">&lt;-</span> <span style="color:#e6db74">&#34;2345&#34;</span>; x
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;12345C&#34;</span>
</span></span></code></pre></div><p><strong>3.2 字符串计算函数</strong></p>
<p><strong>3.2.1 str_count, 字符串计数</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_count</span>(string, pattern <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配的字符。</li>
</ul>
<p>对字符串中匹配的字符计数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_count</span>(<span style="color:#e6db74">&#39;aaa444sssddd&#39;</span>, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#ae81ff">3</span>
</span></span></code></pre></div><p>对字符串向量中匹配的字符计数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> fruit <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;apple&#34;</span>, <span style="color:#e6db74">&#34;banana&#34;</span>, <span style="color:#e6db74">&#34;pear&#34;</span>, <span style="color:#e6db74">&#34;pineapple&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_count</span>(fruit, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_count</span>(fruit, <span style="color:#e6db74">&#34;p&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">3</span>
</span></span></code></pre></div><p>对字符串中的&rsquo;.&lsquo;字符计数，由于.是正则表达式的匹配符，直接判断计数的结果是不对的。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_count</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;a.&#34;</span>, <span style="color:#e6db74">&#34;.&#34;</span>, <span style="color:#e6db74">&#34;.a.&#34;</span>,<span style="color:#66d9ef">NA</span>), <span style="color:#e6db74">&#34;.&#34;</span>)
</span></span><span style="display:flex;"><span>[1]  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">3</span> <span style="color:#66d9ef">NA</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 用fixed匹配字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_count</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;a.&#34;</span>, <span style="color:#e6db74">&#34;.&#34;</span>, <span style="color:#e6db74">&#34;.a.&#34;</span>,<span style="color:#66d9ef">NA</span>), <span style="color:#a6e22e">fixed</span>(<span style="color:#e6db74">&#34;.&#34;</span>))
</span></span><span style="display:flex;"><span>[1]  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">2</span> <span style="color:#66d9ef">NA</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 用\\匹配字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_count</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;a.&#34;</span>, <span style="color:#e6db74">&#34;.&#34;</span>, <span style="color:#e6db74">&#34;.a.&#34;</span>,<span style="color:#66d9ef">NA</span>), <span style="color:#e6db74">&#34;\\.&#34;</span>)
</span></span><span style="display:flex;"><span>[1]  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">2</span> <span style="color:#66d9ef">NA</span>
</span></span></code></pre></div><p><strong>3.2.2 str_length,字符串长度</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_length</span>(string)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
</ul>
<p>计算字符串的长度:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_length</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;I&#34;</span>, <span style="color:#e6db74">&#34;am&#34;</span>, <span style="color:#e6db74">&#34;张丹&#34;</span>, <span style="color:#66d9ef">NA</span>))
</span></span><span style="display:flex;"><span>[1]  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">2</span> <span style="color:#66d9ef">NA</span>
</span></span></code></pre></div><p><strong>3.2.3 str_sort, 字符串值排序，同str_order索引排序</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_sort</span>(x, decreasing <span style="color:#f92672">=</span> <span style="color:#66d9ef">FALSE</span>, na_last <span style="color:#f92672">=</span> <span style="color:#66d9ef">TRUE</span>, locale <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#66d9ef">...</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_order</span>(x, decreasing <span style="color:#f92672">=</span> <span style="color:#66d9ef">FALSE</span>, na_last <span style="color:#f92672">=</span> <span style="color:#66d9ef">TRUE</span>, locale <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#66d9ef">...</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>x: 字符串，字符串向量。</li>
<li>decreasing: 排序方向。</li>
<li>na_last:NA值的存放位置，一共3个值，TRUE放到最后，FALSE放到最前，NA过滤处理</li>
<li>locale:按哪种语言习惯排序</li>
</ul>
<p>对字符串值进行排序。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#75715e"># 按ASCII字母排序</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sort</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#e6db74">&#39;11&#39;</span>), locale <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;en&#34;</span>)  
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;1&#34;</span>  <span style="color:#e6db74">&#34;11&#34;</span> <span style="color:#e6db74">&#34;2&#34;</span>  <span style="color:#e6db74">&#34;a&#34;</span> 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 倒序排序</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sort</span>(<span style="color:#66d9ef">letters</span>,decreasing<span style="color:#f92672">=</span><span style="color:#66d9ef">TRUE</span>)         
</span></span><span style="display:flex;"><span> [1] <span style="color:#e6db74">&#34;z&#34;</span> <span style="color:#e6db74">&#34;y&#34;</span> <span style="color:#e6db74">&#34;x&#34;</span> <span style="color:#e6db74">&#34;w&#34;</span> <span style="color:#e6db74">&#34;v&#34;</span> <span style="color:#e6db74">&#34;u&#34;</span> <span style="color:#e6db74">&#34;t&#34;</span> <span style="color:#e6db74">&#34;s&#34;</span> <span style="color:#e6db74">&#34;r&#34;</span> <span style="color:#e6db74">&#34;q&#34;</span> <span style="color:#e6db74">&#34;p&#34;</span> <span style="color:#e6db74">&#34;o&#34;</span> <span style="color:#e6db74">&#34;n&#34;</span> <span style="color:#e6db74">&#34;m&#34;</span> <span style="color:#e6db74">&#34;l&#34;</span> <span style="color:#e6db74">&#34;k&#34;</span> <span style="color:#e6db74">&#34;j&#34;</span> <span style="color:#e6db74">&#34;i&#34;</span> <span style="color:#e6db74">&#34;h&#34;</span>
</span></span><span style="display:flex;"><span>[20] <span style="color:#e6db74">&#34;g&#34;</span> <span style="color:#e6db74">&#34;f&#34;</span> <span style="color:#e6db74">&#34;e&#34;</span> <span style="color:#e6db74">&#34;d&#34;</span> <span style="color:#e6db74">&#34;c&#34;</span> <span style="color:#e6db74">&#34;b&#34;</span> <span style="color:#e6db74">&#34;a&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 按拼音排序</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sort</span>(<span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#39;你&#39;</span>,<span style="color:#e6db74">&#39;好&#39;</span>,<span style="color:#e6db74">&#39;粉&#39;</span>,<span style="color:#e6db74">&#39;丝&#39;</span>,<span style="color:#e6db74">&#39;日&#39;</span>,<span style="color:#e6db74">&#39;志&#39;</span>),locale <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;zh&#34;</span>)  
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;粉&#34;</span> <span style="color:#e6db74">&#34;好&#34;</span> <span style="color:#e6db74">&#34;你&#34;</span> <span style="color:#e6db74">&#34;日&#34;</span> <span style="color:#e6db74">&#34;丝&#34;</span> <span style="color:#e6db74">&#34;志&#34;</span>
</span></span></code></pre></div><p>对NA值的排序处理</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span> <span style="color:#75715e">#把NA放最后面</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sort</span>(<span style="color:#a6e22e">c</span>(<span style="color:#66d9ef">NA</span>,<span style="color:#e6db74">&#39;1&#39;</span>,<span style="color:#66d9ef">NA</span>),na_last<span style="color:#f92672">=</span><span style="color:#66d9ef">TRUE</span>) 
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;1&#34;</span> <span style="color:#66d9ef">NA</span>  <span style="color:#66d9ef">NA</span>
</span></span><span style="display:flex;"><span> 
</span></span><span style="display:flex;"><span><span style="color:#75715e">#把NA放最前面</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sort</span>(<span style="color:#a6e22e">c</span>(<span style="color:#66d9ef">NA</span>,<span style="color:#e6db74">&#39;1&#39;</span>,<span style="color:#66d9ef">NA</span>),na_last<span style="color:#f92672">=</span><span style="color:#66d9ef">FALSE</span>) 
</span></span><span style="display:flex;"><span>[1] <span style="color:#66d9ef">NA</span>  <span style="color:#66d9ef">NA</span>  <span style="color:#e6db74">&#34;1&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#去掉NA值 </span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_sort</span>(<span style="color:#a6e22e">c</span>(<span style="color:#66d9ef">NA</span>,<span style="color:#e6db74">&#39;1&#39;</span>,<span style="color:#66d9ef">NA</span>),na_last<span style="color:#f92672">=</span><span style="color:#66d9ef">NA</span>)    
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;1&#34;</span>
</span></span></code></pre></div><p><strong>3.3 字符串匹配函数</strong></p>
<p><strong>3.3.1 str_split,字符串分割，同str_split_fixed</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_split</span>(string, pattern, n <span style="color:#f92672">=</span> <span style="color:#66d9ef">Inf</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_split_fixed</span>(string, pattern, n)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配的字符。</li>
<li>n: 分割个数</li>
</ul>
<p>对字符串进行分割。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#e6db74">&#34;abc,123,234,iuuu&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 以,进行分割</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> s1<span style="color:#f92672">&lt;-</span><span style="color:#a6e22e">str_split</span>(val, <span style="color:#e6db74">&#34;,&#34;</span>);s1
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abc&#34;</span>  <span style="color:#e6db74">&#34;123&#34;</span>  <span style="color:#e6db74">&#34;234&#34;</span>  <span style="color:#e6db74">&#34;iuuu&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 以,进行分割，保留2块</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> s2<span style="color:#f92672">&lt;-</span><span style="color:#a6e22e">str_split</span>(val, <span style="color:#e6db74">&#34;,&#34;</span>,<span style="color:#ae81ff">2</span>);s2
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abc&#34;</span>          <span style="color:#e6db74">&#34;123,234,iuuu&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 查看str_split()函数操作的结果类型list</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">class</span>(s1)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;list&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 用str_split_fixed()函数分割，结果类型是matrix</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> s3<span style="color:#f92672">&lt;-</span><span style="color:#a6e22e">str_split_fixed</span>(val, <span style="color:#e6db74">&#34;,&#34;</span>,<span style="color:#ae81ff">2</span>);s3
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]  [,<span style="color:#ae81ff">2</span>]          
</span></span><span style="display:flex;"><span>[1,] <span style="color:#e6db74">&#34;abc&#34;</span> <span style="color:#e6db74">&#34;123,234,iuuu&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">class</span>(s3)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;matrix&#34;</span>
</span></span></code></pre></div><p><strong>3.3.2 str_subset:返回的匹配字符串</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_subset</span>(string, pattern)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配的字符。</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abc&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 全文匹配</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_subset</span>(val, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abc&#34;</span> <span style="color:#e6db74">&#34;cba&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 开头匹配</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_subset</span>(val, <span style="color:#e6db74">&#34;^a&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abc&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 结尾匹配</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_subset</span>(val, <span style="color:#e6db74">&#34;a$&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;cba&#34;</span>
</span></span></code></pre></div><p><strong>3.3.3 word, 从文本中提取单词</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">word</span>(string, start <span style="color:#f92672">=</span> <span style="color:#ae81ff">1L</span>, end <span style="color:#f92672">=</span> start, sep <span style="color:#f92672">=</span> <span style="color:#a6e22e">fixed</span>(<span style="color:#e6db74">&#34; &#34;</span>))
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>start: 开始位置。</li>
<li>end: 结束位置。</li>
<li>sep: 匹配字符。</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;I am Conan.&#34;</span>, <span style="color:#e6db74">&#34;http://fens.me, ok&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 默认以空格分割，取第一个位置的字符串</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">word</span>(val, <span style="color:#ae81ff">1</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;I&#34;</span>               <span style="color:#e6db74">&#34;http://fens.me,&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">word</span>(val, <span style="color:#ae81ff">-1</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;Conan.&#34;</span> <span style="color:#e6db74">&#34;ok&#34;</span>    
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">word</span>(val, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">-1</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;am Conan.&#34;</span> <span style="color:#e6db74">&#34;ok&#34;</span>       
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 以,分割，取第一个位置的字符串 </span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val<span style="color:#f92672">&lt;-</span><span style="color:#e6db74">&#39;111,222,333,444&#39;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">word</span>(val, <span style="color:#ae81ff">1</span>, sep <span style="color:#f92672">=</span> <span style="color:#a6e22e">fixed</span>(<span style="color:#e6db74">&#39;,&#39;</span>))
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;111&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">word</span>(val, <span style="color:#ae81ff">3</span>, sep <span style="color:#f92672">=</span> <span style="color:#a6e22e">fixed</span>(<span style="color:#e6db74">&#39;,&#39;</span>))
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;333&#34;</span>
</span></span></code></pre></div><p><strong>3.3.4 str_detect匹配字符串的字符</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_detect</span>(string, pattern)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配字符。</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abca4&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba2&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 检查字符串向量，是否包括a</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_detect</span>(val, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>[1]  <span style="color:#66d9ef">TRUE</span> <span style="color:#66d9ef">FALSE</span>  <span style="color:#66d9ef">TRUE</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 检查字符串向量，是否以a为开头</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_detect</span>(val, <span style="color:#e6db74">&#34;^a&#34;</span>)
</span></span><span style="display:flex;"><span>[1]  <span style="color:#66d9ef">TRUE</span> <span style="color:#66d9ef">FALSE</span> <span style="color:#66d9ef">FALSE</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 检查字符串向量，是否以a为结尾</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_detect</span>(val, <span style="color:#e6db74">&#34;a$&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#66d9ef">FALSE</span> <span style="color:#66d9ef">FALSE</span> <span style="color:#66d9ef">FALSE</span>
</span></span></code></pre></div><p><strong>3.3.6 str_match,从字符串中提取匹配组</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_match</span>(string, pattern)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_match_all</span>(string, pattern)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配字符。</li>
</ul>
<p>从字符串中提取匹配组</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abc&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 匹配字符a，并返回对应的字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_match</span>(val, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span><span style="display:flex;"><span>[1,] <span style="color:#e6db74">&#34;a&#34;</span> 
</span></span><span style="display:flex;"><span>[2,] <span style="color:#66d9ef">NA</span>  
</span></span><span style="display:flex;"><span>[3,] <span style="color:#e6db74">&#34;a&#34;</span> 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 匹配字符0-9，限1个，并返回对应的字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_match</span>(val, <span style="color:#e6db74">&#34;[0-9]&#34;</span>)
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span><span style="display:flex;"><span>[1,] <span style="color:#66d9ef">NA</span>  
</span></span><span style="display:flex;"><span>[2,] <span style="color:#e6db74">&#34;1&#34;</span> 
</span></span><span style="display:flex;"><span>[3,] <span style="color:#66d9ef">NA</span>  
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 匹配字符0-9，不限数量，并返回对应的字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_match</span>(val, <span style="color:#e6db74">&#34;[0-9]*&#34;</span>)
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>] 
</span></span><span style="display:flex;"><span>[1,] <span style="color:#e6db74">&#34;&#34;</span>   
</span></span><span style="display:flex;"><span>[2,] <span style="color:#e6db74">&#34;123&#34;</span>
</span></span><span style="display:flex;"><span>[3,] <span style="color:#e6db74">&#34;&#34;</span>  
</span></span></code></pre></div><p>从字符串中提取匹配组，以字符串matrix格式返回</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_match_all</span>(val, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span><span style="display:flex;"><span>[1,] <span style="color:#e6db74">&#34;a&#34;</span> 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[2]]
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[3]]
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span><span style="display:flex;"><span>[1,] <span style="color:#e6db74">&#34;a&#34;</span> 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_match_all</span>(val, <span style="color:#e6db74">&#34;[0-9]&#34;</span>)
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[2]]
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span><span style="display:flex;"><span>[1,] <span style="color:#e6db74">&#34;1&#34;</span> 
</span></span><span style="display:flex;"><span>[2,] <span style="color:#e6db74">&#34;2&#34;</span> 
</span></span><span style="display:flex;"><span>[3,] <span style="color:#e6db74">&#34;3&#34;</span> 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[3]]
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>]
</span></span></code></pre></div><p><strong>3.3.7 str_replace，字符串替换</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_replace</span>(string, pattern, replacement)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配字符。</li>
<li>replacement: 用于替换的字符。</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abc&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 把目标字符串第一个出现的a或b，替换为-</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_replace</span>(val, <span style="color:#e6db74">&#34;[ab]&#34;</span>, <span style="color:#e6db74">&#34;-&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;-bc&#34;</span> <span style="color:#e6db74">&#34;123&#34;</span> <span style="color:#e6db74">&#34;c-a&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 把目标字符串所有出现的a或b，替换为-</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_replace_all</span>(val, <span style="color:#e6db74">&#34;[ab]&#34;</span>, <span style="color:#e6db74">&#34;-&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;--c&#34;</span> <span style="color:#e6db74">&#34;123&#34;</span> <span style="color:#e6db74">&#34;c--&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 把目标字符串所有出现的a，替换为被转义的字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_replace_all</span>(val, <span style="color:#e6db74">&#34;[a]&#34;</span>, <span style="color:#e6db74">&#34;\1\1&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;\001\001bc&#34;</span> <span style="color:#e6db74">&#34;123&#34;</span>        <span style="color:#e6db74">&#34;cb\001\001&#34;</span>
</span></span></code></pre></div><p><strong>3.3.8 str_replace_na把NA替换为NA字符串</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_replace_na</span>(string, replacement <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;NA&#34;</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>replacement : 用于替换的字符。</li>
</ul>
<p>把NA替换为字符串</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_replace_na</span>(<span style="color:#a6e22e">c</span>(<span style="color:#66d9ef">NA</span>,<span style="color:#e6db74">&#39;NA&#39;</span>,<span style="color:#e6db74">&#34;abc&#34;</span>),<span style="color:#e6db74">&#39;x&#39;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;x&#34;</span>   <span style="color:#e6db74">&#34;NA&#34;</span>  <span style="color:#e6db74">&#34;abc&#34;</span>
</span></span></code></pre></div><p><strong>3.3.9 str_locate，找到的模式在字符串中的位置。</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_locate</span>(string, pattern)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_locate_all</span>(string, pattern)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配字符。</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abca&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 匹配a在字符串中的位置</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_locate</span>(val, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>[1,]     <span style="color:#ae81ff">1</span>   <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>[2,]    <span style="color:#66d9ef">NA</span>  <span style="color:#66d9ef">NA</span>
</span></span><span style="display:flex;"><span>[3,]     <span style="color:#ae81ff">3</span>   <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 用向量匹配</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_locate</span>(val, <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;a&#34;</span>, <span style="color:#ae81ff">12</span>, <span style="color:#e6db74">&#34;b&#34;</span>))
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>[1,]     <span style="color:#ae81ff">1</span>   <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>[2,]     <span style="color:#ae81ff">1</span>   <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>[3,]     <span style="color:#ae81ff">2</span>   <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 以字符串matrix格式返回</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_locate_all</span>(val, <span style="color:#e6db74">&#34;a&#34;</span>)
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>[1,]     <span style="color:#ae81ff">1</span>   <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>[2,]     <span style="color:#ae81ff">4</span>   <span style="color:#ae81ff">4</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[2]]
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[3]]
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>[1,]     <span style="color:#ae81ff">3</span>   <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 匹配a或b字符，以字符串matrix格式返回</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_locate_all</span>(val, <span style="color:#e6db74">&#34;[ab]&#34;</span>)
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>[1,]     <span style="color:#ae81ff">1</span>   <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>[2,]     <span style="color:#ae81ff">2</span>   <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>[3,]     <span style="color:#ae81ff">4</span>   <span style="color:#ae81ff">4</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[2]]
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[3]]
</span></span><span style="display:flex;"><span>     start end
</span></span><span style="display:flex;"><span>[1,]     <span style="color:#ae81ff">2</span>   <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>[2,]     <span style="color:#ae81ff">3</span>   <span style="color:#ae81ff">3</span>
</span></span></code></pre></div><p><strong>3.3.10 str_extract从字符串中提取匹配模式</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_extract</span>(string, pattern)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_extract_all</span>(string, pattern, simplify <span style="color:#f92672">=</span> <span style="color:#66d9ef">FALSE</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>pattern: 匹配字符。</li>
<li>simplify: 返回值，TRUE返回matrix，FALSE返回字符串向量</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abca4&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba2&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 返回匹配的数字</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_extract</span>(val, <span style="color:#e6db74">&#34;\\d&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;4&#34;</span> <span style="color:#e6db74">&#34;1&#34;</span> <span style="color:#e6db74">&#34;2&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 返回匹配的字符</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_extract</span>(val, <span style="color:#e6db74">&#34;[a-z]+&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abca&#34;</span> <span style="color:#66d9ef">NA</span>     <span style="color:#e6db74">&#34;cba&#34;</span> 
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">c</span>(<span style="color:#e6db74">&#34;abca4&#34;</span>, <span style="color:#ae81ff">123</span>, <span style="color:#e6db74">&#34;cba2&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_extract_all</span>(val, <span style="color:#e6db74">&#34;\\d&#34;</span>)
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;4&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[2]]
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;1&#34;</span> <span style="color:#e6db74">&#34;2&#34;</span> <span style="color:#e6db74">&#34;3&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[3]]
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;2&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_extract_all</span>(val, <span style="color:#e6db74">&#34;[a-z]+&#34;</span>)
</span></span><span style="display:flex;"><span>[[1]]
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;abca&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[2]]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">character</span>(<span style="color:#ae81ff">0</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[[3]]
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;cba&#34;</span>
</span></span></code></pre></div><p><strong>3.4 字符串变换函数</strong></p>
<p><strong>3.4.1 str_conv:字符编码转换</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_conv</span>(string, encoding)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串，字符串向量。</li>
<li>encoding: 编码名。</li>
</ul>
<p>对中文进行转码处理。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#75715e"># 把中文字符字节化</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> x <span style="color:#f92672">&lt;-</span> <span style="color:#a6e22e">charToRaw</span>(<span style="color:#e6db74">&#39;你好&#39;</span>);x
</span></span><span style="display:flex;"><span>[1] c4 e3 ba c3
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 默认win系统字符集为GBK，GB2312为GBK字集，转码正常</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_conv</span>(x, <span style="color:#e6db74">&#34;GBK&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;你好&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_conv</span>(x, <span style="color:#e6db74">&#34;GB2312&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;你好&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 转UTF-8失败</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_conv</span>(x, <span style="color:#e6db74">&#34;UTF-8&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;���&#34;</span>
</span></span><span style="display:flex;"><span>Warning messages<span style="color:#f92672">:</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span><span style="color:#f92672">:</span> In <span style="color:#a6e22e">stri_conv</span>(string, encoding, <span style="color:#e6db74">&#34;UTF-8&#34;</span>) <span style="color:#f92672">:</span>
</span></span><span style="display:flex;"><span>  input data \xffffffc4 in current source encoding could not be converted to Unicode
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span><span style="color:#f92672">:</span> In <span style="color:#a6e22e">stri_conv</span>(string, encoding, <span style="color:#e6db74">&#34;UTF-8&#34;</span>) <span style="color:#f92672">:</span>
</span></span><span style="display:flex;"><span>  input data \xffffffe3\xffffffba in current source encoding could not be converted to Unicode
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">3</span><span style="color:#f92672">:</span> In <span style="color:#a6e22e">stri_conv</span>(string, encoding, <span style="color:#e6db74">&#34;UTF-8&#34;</span>) <span style="color:#f92672">:</span>
</span></span><span style="display:flex;"><span>  input data \xffffffc3 in current source encoding could not be converted to Unicode
</span></span></code></pre></div><p>把unicode转UTF-8</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> x1 <span style="color:#f92672">&lt;-</span> <span style="color:#e6db74">&#34;\u5317\u4eac&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_conv</span>(x1, <span style="color:#e6db74">&#34;UTF-8&#34;</span>)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;北京&#34;</span>
</span></span></code></pre></div><p><strong>3.4.2 str_to_upper,字符串大写转换。</strong></p>
<p>函数定义：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">str_to_upper</span>(string, locale <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_to_lower</span>(string, locale <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">str_to_title</span>(string, locale <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>)
</span></span></code></pre></div><p>参数列表：</p>
<ul>
<li>string: 字符串。</li>
<li>locale:按哪种语言习惯排序</li>
</ul>
<p>字符串大写转换:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> val <span style="color:#f92672">&lt;-</span> <span style="color:#e6db74">&#34;I am conan. Welcome to my blog! http://fens.me&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 全大写</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_to_upper</span>(val)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;I AM CONAN. WELCOME TO MY BLOG! HTTP://FENS.ME&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 全小写</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_to_lower</span>(val)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;i am conan. welcome to my blog! http://fens.me&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 首字母大写</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">str_to_title</span>(val)
</span></span><span style="display:flex;"><span>[1] <span style="color:#e6db74">&#34;I Am Conan. Welcome To My Blog! Http://Fens.Me&#34;</span>
</span></span></code></pre></div><p>字符串在平常的数据处理中经常用过，需要对字符串进行分割、连接、转换等操作，本篇中通过介绍stringr，灵活的字符串处理库，可以有效地提高代码的编写效率。有了好的工具，在用R语言处理字符串就顺手了。</p>
<p>本文转载张丹的粉丝日志，链接：<a href="http://blog.fens.me/r-stringr/">http://blog.fens.me/r-stringr/</a>，</p>

        </section>
    </div>
    <br>
    
    




<span id="/md/2018-04-05-r%E8%AF%AD%E8%A8%80%E5%AD%97%E7%AC%A6%E5%A4%84%E4%B9%8Bstringr%E5%8C%85/" class="leancloud_visitors" data-flag-title="R语言字符处理函数包---stringr包">
  <span class="post-meta-item-text">文章总阅读量 </span>
  <span class="leancloud-visitors-count"><i class="leancloud-visitors-count"></i></span>次;
  <p></p>
</span>



    

    
    
    <button id="edit-button" class="icon-button" type="button" title="Fork and edit" aria-label="Fork and edit" aria-haspopup="true" aria-expanded="false" aria-controls="edit">
        <i class="fa fa-edit">编辑本文</i>
    </button>
    
    
    

    <br>
    <hr>
    <li style="float:left;list-style:none">
        
        <a class="previous" href="/md/2018-03-10-rmd%E4%B8%AD%E6%96%87/"> 上一篇: Windows系统下R Markdown 设置中文pdf完美解决方案</a>
        
    </li>
    <li style="float:right;list-style:none">
        
        <a class="next" href="/md/2018-04-05-r%E8%AF%AD%E8%A8%80%E5%AD%97%E7%AC%A6%E5%A4%84%E7%90%86---%E5%9F%BA%E6%9C%AC%E5%87%BD%E6%95%B0/"> 下一篇: R语言字符处理---基本函数</a>
        
    </li>
     
    
    <script src="/js/copyCode.js"></script>
    <script src="/js/tooltips.js"></script>
    
   
    <script>
    [].slice.call(document.querySelectorAll('table')).forEach(function(el) {
        var wrapper = document.createElement('div');
        wrapper.className = 'table-area';
        el.parentNode.insertBefore(wrapper, el);
        el.parentNode.removeChild(el);
        wrapper.appendChild(el);
        $("table").wrap("<div class='table-area'></div>");
    })
    </script>

    
<br>
<hr>


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-111691389-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', 'UA-111691389-1');
</script>




      
      
      

       
      
      
      <script>
              document.getElementById("edit-button").addEventListener("click", function () {
                  var editWindow = window.open("https:\/\/github.com\/zoushucai\/blogmmm/edit/master/content/md\/2018-04-05-R语言字符处之stringr包.md");
              });</script>
      
          




<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  } 
</script>



    </style>
    <script type="text/javascript">
    function showdiv(){
        document.getElementById("divtocTableOfContents").style.display="block";
        document.getElementById("strHref").innerHTML="目录收起-";
        document.getElementById('divTableOfContents').style.width="22%";
        document.getElementById('divTableOfContents').style.height="55%";
        document.getElementById('divTableOfContents').style.top="25%";
        document.getElementById('divTableOfContents').style.bottom="5%";
        document.getElementById("strHref").href="javascript:hidediv()";
    }
    function hidediv(){
        document.getElementById("divtocTableOfContents").style.display="none";
        document.getElementById("strHref").innerHTML="目录展开+";
        document.getElementById("strHref").href="javascript:showdiv()";
        document.getElementById('divTableOfContents').style.width="10%";
        document.getElementById('divTableOfContents').style.height="5%";
    }
    </script>
</body>

</html>
</div> 







    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-LJ2FmexL77rmGm6SIpxq7y+XA6bkLzGZEgCywzKOZG/ws4va9fUVu2neMjvc3zdv" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            renderMathInElement(document.body, {
            delimiters: [
                            {left: "$$", right: "$$", display: true},
                            {left: "$", right: "$", display: false},
                            {left: "\\(", right: "\\)", display: false},
                            {left: "\\[", right: "\\]", display: true}
                        ]
            });
        });
    </script>













<br>
<div class="inner">
              
            
          
          
  
          
  
  <div id="vcomments"></div>
  
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'HfHPKPkLa0cBEDPcdBAHuqMv-gzGzoHsz',
        appKey: 'r5RJAasN8e0mB9sq6y9pEcX0',
        lang:'zh-CN',
        notify:  false , 
        verify:  false  ,
        avatar:'identicon', 
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

</div>

<br>
<br>
<footer>
    <p style="float:right;margin-right: 5%;margin-top: 0%;">
        &copy; 2022 <a href="https://github.com/zoushucai">zsc</a>
      </p>
</footer>
<br>
<br>


